﻿#region Header

/***************************************************************************
Author : Krishan Kaliraman (www.sharedwith.me)  Date: 1 Aug 2012
***************************************************************************/

#endregion Header
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class ChartActivity
    {
        public string ChartActivityId { get; set; }
        public string ChartActivityTitle { get; set; }
        public string ChartActivityType { get; set; }
        public string ChartActivityOwner { get; set; }
        public string ChartActivityState { get; set; }
        public Dictionary<string, ChartActivityDataItemHelper> ListOfActivitiesToDo = null;
        public Dictionary<string, ChartActivityDataItemHelper> ListOfActivitiesInProgress = null;
        public Dictionary<string, ChartActivityDataItemHelper> ListOfActivitiesDone = null;
        public List<Activity> TasksWithoutEffort = null;

        public ChartActivity(string[] configActivityTypes, Activity committedActivity)
        {
            SetChartActivityInfo(committedActivity);
            FillChartXYValues(configActivityTypes, committedActivity);
        }

        private void SetChartActivityInfo(Activity committedActivity)
        {
            ChartActivityId = committedActivity.Id;
            ChartActivityTitle = committedActivity.Title;
            ChartActivityType = committedActivity.WorkItemType;
            ChartActivityState = committedActivity.State;
            if (!string.IsNullOrEmpty(committedActivity.AssignedTo))
            {
                ChartActivityOwner = committedActivity.AssignedTo;
            }
            else
            {
                ChartActivityOwner = "No Owner";
            }
        }

        private void FillChartXYValues(string[] configActivityTypes,Activity sprintCommittedActivity )
        {
            string[] allActivityTypes = new string[configActivityTypes.Length + 1];
            configActivityTypes.CopyTo(allActivityTypes, 0);
            allActivityTypes[allActivityTypes.Length - 1] = string.Empty;
            ListOfActivitiesToDo = new Dictionary<string, ChartActivityDataItemHelper>();
            ListOfActivitiesInProgress = new Dictionary<string, ChartActivityDataItemHelper>();
            ListOfActivitiesDone = new Dictionary<string, ChartActivityDataItemHelper>();
            TasksWithoutEffort = new List<Activity >();
            FillListOfActivities(ListOfActivitiesToDo ,allActivityTypes ,sprintCommittedActivity );
            FillListOfActivities(ListOfActivitiesInProgress, allActivityTypes, sprintCommittedActivity);
            FillListOfActivities(ListOfActivitiesDone, allActivityTypes, sprintCommittedActivity);
        }

        private void FillListOfActivities(Dictionary<string, ChartActivityDataItemHelper> listOfActivities, string[] allActivityTypes, Activity sprintCommittedActivity)
        {
            foreach (var activityType in allActivityTypes)
            {
                int toDocount = 0,inProgressCount=0,doneCount=0;
                string toolTipForToDoItems = string.Empty, toolTipForInProgressItems = string.Empty, toolTipForDoneItems=string.Empty;
                ChartActivityDataItemHelper newDataItemHelper = new ChartActivityDataItemHelper();
                double remainingEffort = 0;
                double effortOfWorkDone = 0;
                double effortOfWorkInProgress = 0;
                List<Activity> allLinkedChildActivitiesOfCurrentActivityType = sprintCommittedActivity.LinkedChildWorkItems.FindAll(activity => activity.ActivityType == activityType);
                List<Activity> allToDoActivitiesOfCurrenActivityType =sprintCommittedActivity.LinkedChildWorkItems.FindAll(
                        activity => ((activity.ActivityType == activityType)&& (activity .State=="To Do")));
                List<Activity> allInProgressActivitiesOfCurrenActivityType = sprintCommittedActivity.LinkedChildWorkItems.FindAll(
                        activity => ((activity.ActivityType == activityType) && ( activity.State == "In Progress")));
                List<Activity> allDoneActivitiesOfCurrenActivityType = sprintCommittedActivity.LinkedChildWorkItems.FindAll(
                        activity => ((activity.ActivityType == activityType) && (activity.State == "Done")));
                if (allLinkedChildActivitiesOfCurrentActivityType.Count != 0 && allDoneActivitiesOfCurrenActivityType.Count != 0)
                {
                    toolTipForToDoItems += "Total[" + allToDoActivitiesOfCurrenActivityType.Count + "]";
                }
                if (allLinkedChildActivitiesOfCurrentActivityType.Count != 0 && allInProgressActivitiesOfCurrenActivityType.Count != 0)
                {
                    toolTipForInProgressItems += "Total[" + allInProgressActivitiesOfCurrenActivityType.Count + "]";
                }
                if (allLinkedChildActivitiesOfCurrentActivityType.Count != 0 && allDoneActivitiesOfCurrenActivityType.Count != 0)
                {
                    toolTipForDoneItems += "Total[" + allDoneActivitiesOfCurrenActivityType.Count + "]";
                }
                foreach (var linkedChildWorkItem in allLinkedChildActivitiesOfCurrentActivityType)
                {
                    
                    if (linkedChildWorkItem.RemainingWork == Convert.ToDouble(0)&& (linkedChildWorkItem .State=="To Do"|| linkedChildWorkItem .State =="In Progress"))
                    {
                        if (!TasksWithoutEffort.Contains(linkedChildWorkItem))
                        {
                            TasksWithoutEffort.Add(linkedChildWorkItem);
                        }
                    }
                    if (linkedChildWorkItem.State == "To Do")
                    {
                        toDocount = toDocount + 1;
                        remainingEffort = remainingEffort + linkedChildWorkItem.RemainingWork;
                        toolTipForToDoItems += "Task " + linkedChildWorkItem.Id + ":" + linkedChildWorkItem.Title + "[" + linkedChildWorkItem.AssignedTo + "]";
                    }
                    if (linkedChildWorkItem.State == "In Progress")
                    {
                        inProgressCount = inProgressCount + 1;
                        effortOfWorkInProgress = effortOfWorkInProgress + linkedChildWorkItem.RemainingWork;
                        toolTipForInProgressItems += "Task " + linkedChildWorkItem.Id + ":" + linkedChildWorkItem.Title + "[" + linkedChildWorkItem.AssignedTo + "]";
                    }
                    if (linkedChildWorkItem.State == "Done" || linkedChildWorkItem.State == "In Progress")
                    {
                        doneCount = doneCount + 1;
                        effortOfWorkDone = effortOfWorkDone + linkedChildWorkItem.WorkDone;
                        toolTipForDoneItems  += "Task " + linkedChildWorkItem.Id + ":" + linkedChildWorkItem.Title + "[" + linkedChildWorkItem.AssignedTo + "]";
                    }
                    if (toDocount != allToDoActivitiesOfCurrenActivityType.Count && linkedChildWorkItem.State == "To Do")
                    {
                        toolTipForToDoItems += ", ";
                    }
                    if (inProgressCount != allInProgressActivitiesOfCurrenActivityType.Count && linkedChildWorkItem.State == "In Progress")
                    {
                        toolTipForInProgressItems += ", ";
                    }
                    if (doneCount != allDoneActivitiesOfCurrenActivityType.Count && linkedChildWorkItem.State == "Done")
                    {
                        toolTipForDoneItems += ", ";
                    }
                }
                newDataItemHelper.EffortRemaining = remainingEffort;
                newDataItemHelper.EffortInProgress = effortOfWorkInProgress;
                newDataItemHelper.EffortDone = effortOfWorkDone;
                newDataItemHelper.ToolTipForToDoItems = toolTipForToDoItems;
                newDataItemHelper.ToolTipForInProgressItems = toolTipForInProgressItems;
                newDataItemHelper.ToolTipForDoneItems = toolTipForDoneItems;

                if (activityType == string.Empty)
                {
                    listOfActivities.Add("Unknown", newDataItemHelper);
                }
                else
                {
                    listOfActivities.Add(activityType, newDataItemHelper);
                }
            }
        }
    }
}
